package hn.cch.common.core;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class Bean {


    private static Logger logger = LoggerFactory.getLogger(Bean.class);

    /**
     * ClassPathXmlApplicationContext
     * FileSystemXmlApplicationContext
     */

//    private static ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
//    private static ApplicationContext ctx = new FileSystemXmlApplicationContext(Path.BEAN_PATH);

    private static ApplicationContext ctx;

    /**
     * 加载xml配置文件：开启注解
     */
    public static void newBean(String beanPath) {
        ctx = new FileSystemXmlApplicationContext("file:" + beanPath);
        String[] beanNames = ctx.getBeanDefinitionNames();
        for (String beanName:beanNames) {
            //排除springframework
            if (!beanName.startsWith("org.springframework.")) {
                logger.debug("new bean name : " + beanName);
            }
        }
    }

    /**
     * 根据对象名beanName使用spring加载获取对应的对象
     *
     * @param beanName
     * @return
     */
    public static Object getBean(String beanName) {
        logger.debug("get bean name : " + beanName);
        return ctx.getBean(beanName);
    }

    public static void main(String[] args) {

    }


}
